﻿using System;
using System.Collections.Generic;
using System.Text;
using XiaoGang.Soft.XGWMS.DAL_SQL;
using XiaoGang.Soft.XGWMS.COM;
using XiaoGang.Soft.XGWMS.MODEL;

namespace XiaoGang.Soft.XGWMS.BLL
{
    public class BLL_M012_MEMBER
    {
        private readonly DAL_M012_MEMBER dal_M012_MEMBER = new DAL_M012_MEMBER();

        public BLL_M012_MEMBER()
        { }        

        /// <summary>
        /// GetUserList
        /// </summary>
        /// <param name="modMember"></param>
        /// <returns></returns>
        public ReturnValue GetUserList(MOD_M012_MEMBER modMember)
        {
            return dal_M012_MEMBER.GetUserList(modMember);
        }

        /// <summary>
        /// GetMemberBuyDetail
        /// </summary>
        /// <param name="modMember"></param>
        /// <returns></returns>
        public ReturnValue GetMemberBuyDetail(string strUserNO)
        {
            return dal_M012_MEMBER.GetMemberBuyDetail(strUserNO);
        }

        /// <summary>
        /// 添加或更新会员信息
        /// </summary>
        /// <param name="modMember"></param>
        /// <returns></returns>
        public ReturnValue AddOrUpdateMember(MOD_M012_MEMBER modMember)
        {
            ReturnValue returnValue;

            if (modMember.USER_ID != "")
            {
                //更新
                //首先检查非本ID但同会员号的会员
                returnValue = dal_M012_MEMBER.CheckMemberNoWithoutID(modMember.USER_ID, modMember.USER_NO);
                if (!returnValue.IsSuccess)
                {
                    return returnValue;
                }
                if (returnValue.ResultDataSet.Tables[0].Rows.Count > 0)
                {
                    return new ReturnValue(false, "W11H01");//本[@]已经存在或已被逻辑删除!
                }

                //检查是否存在该ID的数据
                returnValue = dal_M012_MEMBER.CheckMemberExist(modMember.USER_ID);
                if (!returnValue.IsSuccess)
                {
                    return returnValue;
                }
                if (returnValue.ResultDataSet.Tables[0].Rows.Count < 1)
                {
                    return new ReturnValue(false, "W11N01");//[@]不存在！
                }
                //进行更新
                return dal_M012_MEMBER.UpdateMember(modMember);
            }
            else
            {
                //新添加
                //首先检查同会员号的会员
                returnValue = dal_M012_MEMBER.CheckMemberNoWithoutID(modMember.USER_ID, modMember.USER_NO);
                if (!returnValue.IsSuccess)
                {
                    return returnValue;
                }
                if (returnValue.ResultDataSet.Tables[0].Rows.Count > 0)
                {
                    return new ReturnValue(false, "W11H01");//本[@]已经存在或已被逻辑删除!
                }
                string UniqueNo = string.Empty;
                if (!DAL_ComProcess.GetUniqueNo(modMember.UPDATER_ID
                    , modMember.TERMINAL_CD
                    , "07"
                    , System.DateTime.Now.Year.ToString("####")
                    , out UniqueNo))
                {
                    return new ReturnValue(false, "E11003");//生成业务编号失败
                }
                modMember.USER_ID = UniqueNo;
                //进行添加
                returnValue = dal_M012_MEMBER.AddMember(modMember);
                if (!returnValue.IsSuccess)
                {
                    return returnValue;
                }
                returnValue.ErrorID = modMember.USER_ID;
                return returnValue;
            }
        }

        /// <summary>
        /// GetUserPhoto
        /// </summary>
        /// <param name="USER_ID"></param>
        /// <returns></returns>
        public ReturnValue GetUserPhoto(string USER_ID)
        {
            return dal_M012_MEMBER.GetUserPhoto(USER_ID);
        }

        /// <summary>
        /// 物理、彻底删除会员
        /// </summary>
        /// <param name="USER_ID"></param>
        /// <returns></returns>
        public ReturnValue DeleteMemberPhysical(string USER_ID)
        {
            ReturnValue returnValue = dal_M012_MEMBER.CheckMemberExist(USER_ID);
            if (!returnValue.IsSuccess)
            {
                return returnValue;
            }
            if (returnValue.ResultDataSet.Tables[0].Rows.Count < 1)
            {
                return new ReturnValue (false, "W11023");//用户名不存在!
            }
            return dal_M012_MEMBER.DelUser(USER_ID);
        }

        public ReturnValue DeleteMemberLogic ( MOD_M012_MEMBER modMember )
        {
            ReturnValue returnValue = dal_M012_MEMBER.CheckMemberExist(modMember.USER_ID);
            if (!returnValue.IsSuccess)
            {
                return returnValue;
            }
            if (returnValue.ResultDataSet.Tables[0].Rows.Count < 1)
            {
                return new ReturnValue (false, "W11023");//用户名不存在!
            }
            return dal_M012_MEMBER.DelUser_Logic (modMember);
        }

        /// <summary>
        /// UpdateUser
        /// </summary>
        /// <param name="modMember"></param>
        /// <returns></returns>
        public ReturnValue UpdateUser(MOD_M012_MEMBER modMember)
        {
            ReturnValue returnValue = dal_M012_MEMBER.CheckMemberExist(modMember.USER_ID);
            if (!returnValue.IsSuccess)
            {
                return returnValue;
            }
            if (returnValue.ResultDataSet.Tables[0].Rows.Count < 1)
            {
                return new ReturnValue(false, "W11023");//The User is not existed!
            }
            return dal_M012_MEMBER.UpdateMember(modMember);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strDateFrom"></param>
        /// <param name="strDateTo"></param>
        /// <returns></returns>
        public ReturnValue GetSalesDetail(string strDateFrom, string strDateTo)
        {
            return dal_M012_MEMBER.GetSalesDetail(strDateFrom, strDateTo);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strDateFrom"></param>
        /// <param name="strDateTo"></param>
        /// <returns></returns>
        public ReturnValue GetBuyDetail(string strDateFrom, string strDateTo)
        {
            return dal_M012_MEMBER.GetBuyDetail(strDateFrom, strDateTo);
        }

        /// <summary>
        /// 盘库明细
        /// </summary>
        /// <param name="strDateFrom"></param>
        /// <param name="strDateTo"></param>
        /// <returns></returns>
        public ReturnValue GetStocktakingDetail(string strDateFrom, string strDateTo)
        {
            return dal_M012_MEMBER.GetStocktakingDetail(strDateFrom, strDateTo);
        }

        /// <summary>
        /// GetBillTotalPrice
        /// </summary>
        /// <param name="strDateFrom"></param>
        /// <param name="strDateTo"></param>
        /// <returns></returns>
        public ReturnValue GetBillTotalPrice(string strDateFrom, string strDateTo)
        {
            return dal_M012_MEMBER.GetBillTotalPrice(strDateFrom, strDateTo);
        }

        /// <summary>
        /// GetBuyTotalPrice
        /// </summary>
        /// <param name="strDateFrom"></param>
        /// <param name="strDateTo"></param>
        /// <returns></returns>
        public ReturnValue GetBuyTotalPrice(string strDateFrom, string strDateTo)
        {
            return dal_M012_MEMBER.GetBuyTotalPrice(strDateFrom, strDateTo);
        }

        /// <summary>
        /// 盘库汇总
        /// </summary>
        /// <param name="strDateFrom"></param>
        /// <param name="strDateTo"></param>
        /// <returns></returns>
        public ReturnValue GetStocktakingTotal(string strDateFrom, string strDateTo)
        {
            return dal_M012_MEMBER.GetStocktakingTotal(strDateFrom, strDateTo);
        }

        public ReturnValue GetMemberrList(string MemberNo)
        {
            return dal_M012_MEMBER.GetMemberrList(MemberNo);
        }

        //public ReturnValue CheckUserNoExist ( string User_NO )
        //{
        //    return dal_M012_MEMBER.CheckUserNoExist(User_NO);
        //}

        public ReturnValue CheckUserNoExist ( string User_NO, string User_ID )
        {
            return dal_M012_MEMBER.CheckUserNoExist (User_NO, User_ID);
        }

        public ReturnValue CheckIsUseUserNo ( string User_NO )
        {
            return dal_M012_MEMBER.CheckIsUseUserNo (User_NO);
        }

        /// <summary>
        /// 取得正在使用的会员
        /// </summary>
        /// <param name="MemberID"></param>
        /// <returns></returns>
        public ReturnValue GetMemberUsed(string MemberID)
        {
            return dal_M012_MEMBER.GetMemberUsed(MemberID);
        }
    }
}
